" Base
" Essential {{{1
" Load ftplugin/ settings
filetype plugin on
" Syntax highlight based on file type
syntax enable
" Only redraw when necessary
set lazyredraw
" Don't use the annoying backup file since I have git
set nobackup
" Stop generating the annoying .swap files
set noswapfile
" Disable modeline for safety (I can use autocmd + vimrc_flagName instead if I really want it)
set nomodeline
" Make spell check more aggressive.
set spelllang=en_us
" Make <c-a> and <c-x> work properly on 07, 08, 09, etc
set nrformats=

" Eye-candy {{{1
function SetDefaultColorschemeFunction()
  if !empty($ENV_MAC)
    set background=dark
    colorscheme gruvbox
  else
    " Explicitly use 256 color, mac iTerm sets it for me {{{2
    set t_Co=256
    set background=dark
    colorscheme gruvbox
  endif
endfunction
silent! call SetDefaultColorschemeFunction()

" Change cursor shape between insert and normal mode in iTerm and tmux {{{2
if exists('$TMUX')
  let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
  let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
elseif $TERM_PROGRAM =~ "iTerm"
  let &t_SI = "\<Esc>]50;CursorShape=1\x7" " Vertical bar in insert mode
  let &t_EI = "\<Esc>]50;CursorShape=0\x7" " Block in normal mode
endif

" Miscellaneous {{{2
" Cursor position on the bottom right corner
set ruler

" Show current file name on status bar
set laststatus=2

" Set relative line numbering
set relativenumber
" Set absolute line numbering for the current line
set number

" New horizontal splits spawns below
set splitbelow
" New vertical splits spawns to the right
set splitright

" Preview 5 lines when scrolling
set scrolloff=5

" Wrap long text to the next display line
set wrap

" Wrap long text by breaking between words
set linebreak

" Highlight current line
set cursorline

" show invisibles
set list
set listchars=tab:▸\ ,extends:❯,precedes:❮,nbsp:.,trail:·

" Edit {{{1
" Automatically indent
set autoindent
" Smartly indent, helpful for coding because it takes care of brackets
set smartindent

" 2-space tabing
set tabstop=2
" treat 2 spaces as one deletion when deleting
set softtabstop=2
" Set shiftwidth the same value as tabstop
set shiftwidth=0

" Use spaces for tabs
set expandtab

" Search {{{1
" Highlight search results
set hlsearch

" Case insensitive
set ignorecase
" Toggle on case sensitive when the search pattern contains an upper-case letter
set smartcase

" Incremental search to preview matches
set incsearch

" Default replace to be global, as opposed to only the current line
set gdefault

" Causes search to wrap back to the top of the file when reached bottom
set wrapscan

" Ex command line {{{1
" In ex mode, hit <tab> will show all available options
set wildmenu
" Version control
set wildignore+=.hg,.git,.svn
" LaTeX intermediate files
set wildignore+=*.aux,*.out,*.toc
" binary images
set wildignore+=*.jpg,*.bmp,*.gif,*.png,*.jpeg
" compiled object files
set wildignore+=*.o,*.obj,*.exe,*.dll,*.manifest
" compiled spelling word lists
set wildignore+=*.spl
" Vim swap files
set wildignore+=*.sw?
" OSX junk
set wildignore+=*.DS_Store
" Lua byte code
set wildignore+=*.luac
" Python byte code
set wildignore+=*.pyc
" Merge resolution files
set wildignore+=*.orig
" Java class files
set wildignore+=*.class

" Universal auto commands {{{1
" Return to last edit position {{{2
autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

" Only show cursorline for the current window {{{2
autocmd WinEnter,BufEnter * setlocal cursorline
autocmd WinLeave * setlocal nocursorline

" Turn off paste mode on existing insert mode {{{2
autocmd InsertLeave * set nopaste

" Handle non-UTF-8 characters on save {{{2
autocmd BufWritePre * ReplaceUncommonCharacters

" Set proper auto write and hidden for no name buffers and named buffers {{{2
function SetAutoWriteAndHiddenFunction()
  if bufname('%') != ''
    " named buffer associated with a file
    " save on switching buffers
    setlocal autowrite
    setlocal autowriteall
    setlocal nohidden
  else
    " no name buffer
    " there is nothing to save
    setlocal noautowrite
    setlocal noautowriteall
    " this allows me to switch buffers easily
    setlocal hidden
  endif
endfunction
autocmd WinEnter,BufEnter * silent! call SetAutoWriteAndHiddenFunction()

" Miscellaneous {{{1
" History {{{2
" More history
set history=100

" No history for netrw
let g:netrw_dirhistmax=0

" File IO {{{2
" automatically reload changed file
set autoread

" Folding {{{2
" Don't fold on startup
set nofoldenable

" Fold on marker always!
set foldmethod=marker

" Maximum levels to fold is 10 (never should exceed)
set foldnestmax=10

